<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <script src="../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../res/js/jquery-3.1.1.min.js" charset="utf-8"></script>
    <script src="../../res/js/Tools.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>增加和修改</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="example" id="newsForm">
    <!--<input type='hidden' name='roleId' id='roleId'>-->
    <input type='hidden' name='rivId' id='rivId'>
    <div class="layui-form-item">
        <label class="layui-form-label">对手姓名</label>
        <div class="layui-input-block">
            <input type="text" name="rivName" lay-verify="title" autocomplete="off" placeholder="请输入对手姓名" class="layui-input" id="rivName">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">请选择性别</label>
        <div class="layui-input-block" style="width:200px;">

            <select name="rivSex" id="rivSex">
                <option value="男" >男</option>
                <option value="女" >女</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">体育项目</label>
        <div class="layui-input-block">
            <select name="sportId" id="sportId">
                <option value="">请选择对手的体育项目</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">缺点</label>
        <div class="layui-input-block">
            <input type="text" name="rivFailing" id="rivFailing" lay-verify="faven" autocomplete="off" placeholder="缺点" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">技术风格</label>
        <div class="layui-input-block">
            <input type="text" name="rivTechnique" id="rivTechnique" lay-verify="teven" autocomplete="off" placeholder="技术风格" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">战术风格</label>
        <div class="layui-input-block">
            <input type="text" name="rivTactics" id="rivTactics" lay-verify="riven" autocomplete="off" placeholder="战术风格" class="layui-input">
        </div>
    </div>

    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">排序</label>-->
        <!--<div class="layui-input-block">-->
            <!--<input type="text" name="sort" id="sort" lay-verify="sover" autocomplete="off" placeholder="排序" class="layui-input">-->
        <!--</div>-->
    <!--</div>-->

    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">备注</label>-->
        <!--<div class="layui-input-block">-->
            <!--<input type="text" name="remark" id="remark" lay-verify="title" autocomplete="off" placeholder="备注" class="layui-input">-->
        <!--</div>-->
    <!--</div>-->


    <div class="layui-form-item">
        <label class="layui-form-label">应对方案</label>
        <div class="layui-input-block">
            <input type="text" name="rivIdea" id="rivIdea" lay-verify="title" autocomplete="off" placeholder="应对方案" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>

</form>

<script>



    layui.use(['form', 'layedit', 'laydate','upload'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,upload = layui.upload ;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //findSport();

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 2){
                    return '名字至少得2个字符啊';
                }
            }
            ,sover: [
                /^\d+$|^\d+[.]?\d+$/
                ,'只能是数字'

            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });
        //监听提交
        form.on('submit(demo1)', function(data){
            var id = "";
            $("input[name='roleId']:checked").each(function(index,items){
                id+=$(this).val()+",";
            });
            $("#roleId").val(id);
            var formData = new FormData(document.getElementById("newsForm"));
            //传输路径
            var url = "../../AtsRival/AddOrUpdate";
            $.ajax({
                url:url,
                type:"post", // 传输类型
                data:formData, // 数据
                async:false, // 全局同步
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){

                    if(obj.num==1){
                        layer.msg(obj.msg,{
                            icon:1,
                            time:1000,
                        },function(){
                            var indexs = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(indexs);
                        });
                        return false ;
                    }
                },
                error:function(e){
                    alert("失败了");
                }
            });
            return false
        });
        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');

        });

        //获取主页面传过来的id
        var id = getQueryString("id");

        if(id!=null){

            var url = "../../AtsRival/findById";
            var data = {id:id};
            $.post(url,data,function(obj){

                $("#rivId").val(obj.rivId);
                $("#rivName").val(obj.rivName);
                $("#rivSex option[value='" + obj.rivSex + "']").attr("selected", "selected");

               // $("#sportId option[value='" + obj.sportId + "']").attr("selected", "selected");
                //$('#rivSex').append("<option value='"+obj.rivSex+"'>"+obj.rivSex+"</option>");
                $("#rivFailing").val(obj.rivFailing);
                $("#rivTechnique").val(obj.rivTechnique);
                $("#rivTactics").val(obj.rivTactics);
                $("#rivIdea").val(obj.rivIdea);
                // $("#sort").val(obj.sort);
                // $("#remark").val(obj.remark);
                findBySp(obj.sportId);

            },'json')
        }else{

            findSport();
         }





        function findBySp(id) {
            var url = "../../AtsRival/findBySp";
            var data={sportId:id};
            $.post(url,data,function (obj) {
                //清除之前的哦option
                $("#sportId").empty();
                //$("#sportId option[value='" + obj.sportName + "']").attr("selected", "selected");
                $("#sportId").append("<option value='"+obj.sportId+"'>"+obj.sportName+"</option>");

               // findSport();
                layui.form.render("select");

            },"json");

        }

        function findSport() {
            var form = layui.form;
            var url = "../../AtsRival/findSport";
            $.post(url,null,function (obj) {
                $.each(obj,function (i,item) {
                    $("#sportId").append("<option value='"+item.sportId+"'>"+item.sportName+"</option>");
                    form.render();
                })
            },"json");
        }




    });




</script>


<script>




</script>
</body>
</html>